package ynu.edu.dish.service.impl;

import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import ynu.edu.common.exception.BusinessException;
import ynu.edu.dish.dto.DishSpecDTO;
import ynu.edu.dish.entity.DishSpec;
import ynu.edu.dish.repository.DishSpecRepository;
import ynu.edu.dish.service.DishSpecService;
import ynu.edu.dish.vo.DishSpecVO;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 菜品规格服务实现类
 */
@Service
@RequiredArgsConstructor
public class DishSpecServiceImpl implements DishSpecService {

    private final DishSpecRepository dishSpecRepository;

    @Override
    @Transactional
    public DishSpecVO createDishSpec(DishSpecDTO dishSpecDTO) {
        // 创建新的菜品规格
        DishSpec dishSpec = new DishSpec();
        BeanUtils.copyProperties(dishSpecDTO, dishSpec);

        // 保存菜品规格
        dishSpec = dishSpecRepository.save(dishSpec);

        // 转换为VO并返回
        return convertToVO(dishSpec);
    }

    @Override
    @Transactional
    public DishSpecVO updateDishSpec(DishSpecDTO dishSpecDTO) {
        // 检查菜品规格是否存在
        DishSpec dishSpec = dishSpecRepository.findById(dishSpecDTO.getId())
                .orElseThrow(() -> new BusinessException("菜品规格不存在"));

        // 更新菜品规格
        BeanUtils.copyProperties(dishSpecDTO, dishSpec);

        // 保存菜品规格
        dishSpec = dishSpecRepository.save(dishSpec);

        // 转换为VO并返回
        return convertToVO(dishSpec);
    }

    @Override
    public DishSpecVO getDishSpecById(Long id) {
        // 查询菜品规格
        DishSpec dishSpec = dishSpecRepository.findById(id)
                .orElseThrow(() -> new BusinessException("菜品规格不存在"));

        // 转换为VO并返回
        return convertToVO(dishSpec);
    }

    @Override
    @Transactional
    public boolean deleteDishSpec(Long id) {
        // 检查菜品规格是否存在
        if (!dishSpecRepository.existsById(id)) {
            throw new BusinessException("菜品规格不存在");
        }

        // 删除菜品规格
        dishSpecRepository.deleteById(id);

        return true;
    }

    @Override
    public List<DishSpecVO> getDishSpecsByDishId(Long dishId) {
        // 查询菜品规格列表
        List<DishSpec> dishSpecs = dishSpecRepository.findByDishId(dishId);

        // 转换为VO列表并返回
        return dishSpecs.stream()
                .map(this::convertToVO)
                .collect(Collectors.toList());
    }

    @Override
    public List<DishSpecVO> getDishSpecsByDishIdAndStatus(Long dishId, Integer status) {
        // 查询菜品规格列表
        List<DishSpec> dishSpecs = dishSpecRepository.findByDishIdAndStatus(dishId, status);

        // 转换为VO列表并返回
        return dishSpecs.stream()
                .map(this::convertToVO)
                .collect(Collectors.toList());
    }

    @Override
    @Transactional
    public boolean updateDishSpecStatus(Long id, Integer status) {
        // 检查菜品规格是否存在
        DishSpec dishSpec = dishSpecRepository.findById(id)
                .orElseThrow(() -> new BusinessException("菜品规格不存在"));

        // 更新状态
        dishSpec.setStatus(status);
        dishSpecRepository.save(dishSpec);

        return true;
    }

    @Override
    @Transactional
    public List<DishSpecVO> batchCreateDishSpecs(List<DishSpecDTO> dishSpecDTOs) {
        // 批量创建菜品规格
        List<DishSpec> dishSpecs = new ArrayList<>();
        for (DishSpecDTO dishSpecDTO : dishSpecDTOs) {
            DishSpec dishSpec = new DishSpec();
            BeanUtils.copyProperties(dishSpecDTO, dishSpec);
            dishSpecs.add(dishSpec);
        }

        // 保存菜品规格
        dishSpecs = dishSpecRepository.saveAll(dishSpecs);

        // 转换为VO列表并返回
        return dishSpecs.stream()
                .map(this::convertToVO)
                .collect(Collectors.toList());
    }

    @Override
    @Transactional
    public boolean deleteAllByDishId(Long dishId) {
        // 删除菜品规格
        dishSpecRepository.deleteByDishId(dishId);

        return true;
    }

    /**
     * 将实体转换为VO
     *
     * @param dishSpec 菜品规格实体
     * @return 菜品规格VO
     */
    private DishSpecVO convertToVO(DishSpec dishSpec) {
        DishSpecVO dishSpecVO = new DishSpecVO();
        BeanUtils.copyProperties(dishSpec, dishSpecVO);
        return dishSpecVO;
    }
} 